﻿using Model;
using PrintLabel;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 纺织贸易管理系统;

namespace Tools
{
    public class 打印检验报告
    {
        public static void PrintReport(PrintUseful Useful, List<JuanHaoTable> juans, string templatename, string company)
        {
            //juans = juans.OrderBy(x => x.PiHao).ToList();
            var juandt = CreateDT();
            InitDT(ref juandt, juans);
            DataSet ds = new DataSet();
            ds.Tables.Add(juandt);
            ///生成疵点对照字符串
            var cidiandt = new DataTable("疵点名称");
            cidiandt.Columns.Add("疵点名称", typeof(string));
            cidiandt.Columns.Add("疵点英文", typeof(string));
            cidiandt.Rows.Add();
            var cidianlist = string.Empty;
            foreach (var cidian in dbContext.dbcontext.Queryable<CiDianNameTable>().ToList())
            {
                cidianlist += cidian.Daihao + cidian.CiDianName + cidian.EnglishName + "||";
            }
            cidianlist.Remove(cidianlist.Length - 1, 1);
            cidiandt.Rows[0][0] = cidianlist;
            ///
            ds.Tables.Add(cidiandt);
            ds.Tables.Add(CreateTotal(juans));
            //创建公司信息表
            var dt = new DataTable("公司名称");
            dt.Columns.Add("用户公司名称");
            dt.Columns.Add("用户公司电话");
            dt.Columns.Add("公司税号");
            dt.Columns.Add("公司地址");
            dt.Columns.Add("开户银行");
            dt.Columns.Add("银行账号");
            dt.Columns.Add("电子邮箱");
            dt.Columns.Add("银行行号");
            var gsinfo = dbContext.dbcontext.Queryable<info>().First(x => x.gsmc == company);
            if (gsinfo == null)
            {
               gsinfo=new info ();
            }
            dt.Rows.Add();
            dt.Rows[0]["用户公司名称"] = gsinfo.gsmc;
            dt.Rows[0]["用户公司电话"] = gsinfo.GHSMC;
            dt.Rows[0]["公司税号"] = gsinfo.TaxNum;
            dt.Rows[0]["公司地址"] = gsinfo.Address;
            dt.Rows[0]["开户银行"] = gsinfo.BankName;
            dt.Rows[0]["银行账号"] = gsinfo.BankNum;
            dt.Rows[0]["电子邮箱"] = gsinfo.Email;
            dt.Rows[0]["银行行号"] = gsinfo.BankNo;
            ds.Tables.Add(dt);

            var template = string.Empty;
            switch (templatename)
            {
                case "中文":
                    template = "CheckDoc.frx";
                    break;
                case "英文":
                    template = "EnglishCheckDoc.frx";
                    break;
                case "中性中文":
                    template = "neutralCheckDoc.frx";
                    break;
                case "中性英文":
                    template = "neutralEnglishCheckDoc.frx";
                    break;
            }
            var path = Application.StartupPath + "\\Labels\\CheckDoc.frx" ;
            if (File.Exists(path))
            {
                ReportService.Print(ds, new PrintSetting() { PrintUseful = Useful, Path = path });
            }
        }
        /// <summary>
        /// 生成表结构
        /// </summary>
        /// <returns></returns>
        private static DataTable CreateDT()
        {
            DataTable dt = new DataTable("检验报告");
            dt.Columns.Add("缸号", typeof(string));
            dt.Columns.Add("客户缸号", typeof(string));
            dt.Columns.Add("品名", typeof(string));
            dt.Columns.Add("物料编号", typeof(string));
            dt.Columns.Add("卷号", typeof(string));
            dt.Columns.Add("颜色", typeof(string));
            dt.Columns.Add("实长", typeof(int));
            dt.Columns.Add("拼接位置", typeof(decimal));
            dt.Columns.Add("幅宽", typeof(string));
            dt.Columns.Add("等级", typeof(string));
            dt.Columns.Add("赠送", typeof(decimal));
            dt.Columns.Add("检验日期", typeof(DateTime));
            dt.Columns.Add("批号", typeof(string));
            dt.Columns.Add("订单号", typeof(string));
            dt.Columns.Add("合同号", typeof(string));
            dt.Columns.Add("单位", typeof(string));
            dt.Columns.Add("客户名称", typeof(string));
            dt.Columns.Add("款号", typeof(string));
            dt.Columns.Add("英文名", typeof(string));
            dt.Columns.Add("英文单位", typeof(string));
            dt.Columns.Add("染厂长度", typeof(decimal));
            dt.Columns.Add("检验员", typeof(string));
            dt.Columns.Add("机台号", typeof(string));
            for (int col = 0; col < 18; col++)
            {
                dt.Columns.Add("代码" + col.ToString(), typeof(string));
                dt.Columns.Add("位置" + col.ToString(), typeof(decimal));
                dt.Columns.Add("扣分" + col.ToString(), typeof(int));
            }
            dt.Columns.Add("备注", typeof(string));
            dt.Columns.Add("总分", typeof(int));
            dt.Columns.Add("平均分", typeof(decimal));
            return dt;
        }
        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <param name="juandt"></param>
        /// <param name="juans"></param>
        private static void InitDT(ref DataTable juandt, List<JuanHaoTable> juans)
        {
            var chidiannames = dbContext.dbcontext.Queryable<CiDianNameTable>().ToList();
            int index = 0;
            DateTime jianyanriqi = juans.OrderBy(x => x.rq).ToList()[0].rq;
            foreach (var juan in juans)
            {
                var cidians = dbContext.dbcontext.Queryable<ChiDianTable>().Where(x => x.JuanHao == juan.JuanHao).ToList();
                if (juan.SumKouFeng == 0)
                {
                    juan.SumKouFeng = cidians.Sum(x => x.KouFeng);
                    if (juan.SumKouFeng == 0)
                    {
                        juan.SumKouFeng = cidians.Sum(x => x.ShuLiang);
                    }
                }
                AddNewRow(juandt, juan, jianyanriqi);
                index = juandt.Rows.Count - 1;

                int col = 0;
                foreach (var cidian in cidians)
                {
                    if (!cidian.ChiDianName.Contains("通坯"))
                    {
                        if (cidian.IsALL == "通坯")
                        {
                            juandt.Rows[index]["备注"] += cidian.EdgeDistance + "CM" + cidian.ChiDianName + "通匹" + "(scratch 3.5cm from " + dbContext.dbcontext.Queryable<CiDianNameTable>().First(x => x.CiDianName == cidian.ChiDianName).EnglishName + ")";
                        }
                        juandt.Rows[index]["代码" + col.ToString()] = chidiannames.FirstOrDefault(x => x.CiDianName == cidian.ChiDianName).Daihao;
                        juandt.Rows[index]["位置" + col.ToString()] = cidian.WeiZhi;
                        juandt.Rows[index]["扣分" + col.ToString()] = cidian.ShuLiang == 0 ? cidian.KouFeng : cidian.ShuLiang;
                    }
                    else
                    {
                        juandt.Rows[index]["备注"] += cidian.EdgeDistance + "CM" + cidian.ChiDianName.Replace("坯", "匹") + "( " + cidian.EdgeDistance + " cm from " + dbContext.dbcontext.Queryable<CiDianNameTable>().First(x => x.CiDianName == cidian.ChiDianName.Split('(')[0]).EnglishName + ")";
                    }
                    col++;
                    if (col == 18)
                    {
                        index++;
                        AddNewRow(juandt, juan, jianyanriqi);
                        col = 0;
                    }
                }
            }
        }
        /// <summary>
        /// 添加一行
        /// </summary>
        /// <param name="juandt"></param>
        /// <param name="juan"></param>
        private static void AddNewRow(DataTable juandt, JuanHaoTable juan, DateTime jianyanriqi)
        {
            juandt.Rows.Add();
            var index = juandt.Rows.Count - 1;
            juandt.Rows[index]["缸号"] = juan.GangHao;
            juandt.Rows[index]["卷号"] = juan.PiHao;
            juandt.Rows[index]["品名"] = juan.SampleName;
            juandt.Rows[index]["物料编号"] = juan.SampleNum;
            juandt.Rows[index]["颜色"] = juan.yanse;
            juandt.Rows[index]["实长"] = juan.biaoqianmishu;
            juandt.Rows[index]["拼接位置"] = 0;
            juandt.Rows[index]["幅宽"] = juan.Menfu;
            juandt.Rows[index]["总分"] = juan.SumKouFeng;
            var menfu = juan.Menfu.ToDecimal();
            menfu = (menfu == (decimal)0 ? 140 : menfu);
            menfu = menfu < 100 ? menfu : menfu / (decimal)2.54;
            juandt.Rows[index]["平均分"] = (juan.SumKouFeng * 3600) / ((juan.Danwei == "米" ? juan.biaoqianmishu / (decimal)0.9144 : juan.biaoqianmishu) * (menfu));
            juandt.Rows[index]["等级"] = juan.DengJI;
            juandt.Rows[index]["赠送"] = juan.JiaJian;
            juandt.Rows[index]["检验日期"] = jianyanriqi;
            juandt.Rows[index]["批号"] = juan.Houzhengli;
            juandt.Rows[index]["单位"] = juan.Danwei;
            juandt.Rows[index]["订单号"] = juan.OrderNum;
            juandt.Rows[index]["合同号"] = juan.Hetonghao;
            juandt.Rows[index]["客户名称"] = juan.CustomerName;
            juandt.Rows[index]["款号"] = juan.kuanhao;
            juandt.Rows[index]["客户缸号"] = juan.CustomerLotNo;
            juandt.Rows[index]["英文名"] = juan.EnglishName;
            juandt.Rows[index]["英文单位"] = juan.Danwei == "米" ? "M" : "Y";
            juandt.Rows[index]["染厂长度"] = string.IsNullOrEmpty(juan.DyerLength) ? 0 : juan.DyerLength.ToDecimal ();
            juandt.Rows[index]["机台号"] = juan.MachineID;
            juandt.Rows[index]["检验员"] = juan.CheckID;
        }
        private static DataTable CreateTotal(List<JuanHaoTable> juans)
        {
            var dt = new DataTable("合计");
            DataColumn ACount = new DataColumn("A匹数", typeof(int));
            DataColumn ATotal = new DataColumn("A米数", typeof(int));
            DataColumn BCount = new DataColumn("B匹数", typeof(int));
            DataColumn BTotal = new DataColumn("B米数", typeof(int));
            DataColumn CCount = new DataColumn("C匹数", typeof(int));
            DataColumn CTotal = new DataColumn("C米数", typeof(int));
            DataColumn DCount = new DataColumn("D匹数", typeof(int));
            DataColumn DTotal = new DataColumn("D米数", typeof(int));
            DataColumn AllTotal = new DataColumn("ALL米数", typeof(int));
            dt.Columns.Add(ACount);
            dt.Columns.Add(ATotal);
            dt.Columns.Add(BCount);
            dt.Columns.Add(BTotal);
            dt.Columns.Add(CCount);
            dt.Columns.Add(CTotal);
            dt.Columns.Add(DCount);
            dt.Columns.Add(DTotal);
            dt.Columns.Add(AllTotal);
            dt.Rows.Add();
            dt.Rows[0][ACount] = juans.Where(x => x.DengJI == "A").Count();
            dt.Rows[0][ATotal] = juans.Where(x => x.DengJI == "A").Sum(x => x.biaoqianmishu);
            dt.Rows[0][BCount] = juans.Where(x => x.DengJI == "B").Count();
            dt.Rows[0][BTotal] = juans.Where(x => x.DengJI == "B").Sum(x => x.biaoqianmishu);
            dt.Rows[0][CCount] = juans.Where(x => x.DengJI == "C").Count();
            dt.Rows[0][CTotal] = juans.Where(x => x.DengJI == "C").Sum(x => x.biaoqianmishu);
            dt.Rows[0][DCount] = juans.Where(x => x.DengJI == "D").Count();
            dt.Rows[0][DTotal] = juans.Where(x => x.DengJI == "D").Sum(x => x.biaoqianmishu);
            dt.Rows[0][AllTotal] = juans.Sum(x => x.biaoqianmishu);
            return dt;
        }
    }
}
